package com.smasher.component.helper

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.AudioAttributes
import android.net.Uri
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import com.smasher.widget.R
import java.util.Random

open class NotificationHelper {


    fun createChannel(
        id: String, name: String, description: String
    ) {
        val importance = NotificationManagerCompat.IMPORTANCE_NONE
        createChannel(
            id, name, description, null, false, null, null, false, false, importance
        )
    }

    fun createChannel(
        id: String, name: String, description: String, importance: Int
    ) {
        createChannel(
            id, name, description, null, false, null, null, false, false, importance
        )
    }

    fun createChannel(
        id: String, name: String, description: String, groupId: String
    ) {
        val importance = NotificationManagerCompat.IMPORTANCE_NONE
        createChannel(
            id, name, description, groupId, false, null, null, false, false, importance
        )
    }

    fun createChannel(
        id: String, name: String, description: String, groupId: String, importance: Int
    ) {
        createChannel(
            id, name, description, groupId, false, null, null, false, false, importance
        )
    }

    fun createChannel(
        id: String, name: String, description: String, groupId: String, showBadge: Boolean
    ) {
        val importance = NotificationManagerCompat.IMPORTANCE_NONE
        createChannel(
            id, name, description, groupId, showBadge, null, null, false, false, importance
        )
    }

    fun createChannel(
        id: String,
        name: String,
        description: String,
        groupId: String?,
        showBadge: Boolean,
        sound: Uri?,
        audioAttributes: AudioAttributes?,
        lightEnable: Boolean,
        vibrationEnabled: Boolean,
        importance: Int
    ): NotificationChannelCompat {
        val vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
        val builder = NotificationChannelCompat.Builder(id, importance)
        builder.setName(name)
            .setDescription(description)
            .setGroup(groupId)
            .setShowBadge(showBadge)
            .setSound(sound, audioAttributes)
            .setLightsEnabled(lightEnable)
            .setVibrationEnabled(vibrationEnabled)
            .setVibrationPattern(vibrationPattern)
        return builder.build()
    }

    fun createNotificationChannel(context: Context, channels: List<NotificationChannelCompat>) {
        val notificationManager = NotificationManagerCompat.from(context)
        notificationManager.createNotificationChannelsCompat(channels)
    }


    fun checkNotifySetting(context: Context): Boolean {
        val notificationManager = NotificationManagerCompat.from(context)
        // areNotificationsEnabled方法的有效性官方只最低支持到API 19，低于19的仍可调用此方法不过只会返回true，即默认为用户已经开启了通知。
        return notificationManager.areNotificationsEnabled()
    }


    fun notifyMessage(context: Context, channelId: String, title: String, message: String): Int {

        val builder = NotificationCompat.Builder(context, channelId)
        builder.color = ContextCompat.getColor(context, R.color.colorPrimaryDark)
        builder.setSmallIcon(providerSmallIcon())
        builder.setLargeIcon(providerLargeIcon(context))
        builder.setContentTitle(title)
        builder.setContentText(message)
        builder.setWhen(System.currentTimeMillis())
        builder.setAutoCancel(true)
        val random = Random()
        val id = random.nextInt()
        val notificationManager = NotificationManagerCompat.from(context)
        if (ActivityCompat.checkSelfPermission(
                context,
                Manifest.permission.POST_NOTIFICATIONS
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return 0
        }
        notificationManager.notify(id, builder.build())
        return id
    }

    open fun providerSmallIcon(): Int {
        return com.smasher.component.R.mipmap.ic_launcher
    }

    open fun providerLargeIcon(context: Context): Bitmap {
        return BitmapFactory.decodeResource(
            context.resources,
            com.smasher.component.R.mipmap.ic_launcher
        )
    }
}